

//
//  LYPrivacySettingController.m
//  SelectCity
//
//  Created by liuyan on 2019/3/29.
//  Copyright © 2019年 liuyan. All rights reserved.
//

#import "LYPrivacySettingController.h"
#import "LYPrivacySettingCell.h"

@interface LYPrivacySettingController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, strong) UITableView *tableview;
@property (nonatomic, strong) NSMutableArray *dataArray;

@property (nonatomic, strong) UITextField *keywordTF;

@end

@implementation LYPrivacySettingController

-(NSMutableArray *)dataArray{
    if (!_dataArray) {
        _dataArray = [NSMutableArray arrayWithArray:self.shieldArray];
    }
    return _dataArray;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    
    self.navigationItem.title = @"屏蔽企业";
    
    [self createSubview];
}

- (void)createSubview{
    self.tableview = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, WIDTH,HEIGHT-HeightOfStatusBarAndNavigationBar) style:UITableViewStyleGrouped];
    self.tableview.delegate = self;
    self.tableview.dataSource = self;
    self.tableview.showsVerticalScrollIndicator = NO;
    self.tableview.estimatedRowHeight = 0;
    self.tableview.estimatedSectionHeaderHeight = 0;
    self.tableview.estimatedSectionFooterHeight = 0;
    self.tableview.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableview.backgroundColor = [UIColor whiteColor];
    self.tableview.sectionIndexBackgroundColor = [UIColor clearColor];
    
    [self.tableview registerClass:[LYPrivacySettingCell class] forCellReuseIdentifier:kCellIdentifier_LYPrivacySettingCell];
    [self.view addSubview:self.tableview];
    
    UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, 60)];
    headerView.backgroundColor = [UIColor whiteColor];
    self.tableview.tableHeaderView = headerView;
    
    UILabel *detailLabel = [[UILabel alloc] initWithFrame:CGRectMake(15,25, WIDTH - 30, 15)];
    detailLabel.text = [NSString stringWithFormat:@"最多可屏蔽%li家企业",self.shieldMax];
    detailLabel.font = [UIFont boldSystemFontOfSize:12];
    detailLabel.textColor = RGB(34, 34, 34);
    [headerView addSubview:detailLabel];
    
    UIView *footer = [[UIView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, 70)];
    footer.backgroundColor = [UIColor whiteColor];
    self.tableview.tableFooterView = footer;
    
    UIButton * addBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    addBtn.frame = CGRectMake( WIDTH - 100, 11, 80 , 23);
    addBtn.backgroundColor = THEMECOLOR_OF_GREEN;
    [addBtn setTitle:@"加入屏蔽" forState:UIControlStateNormal];
    [addBtn setTitleColor:COLOR_OF_WHITE forState:UIControlStateNormal];
    addBtn.layer.cornerRadius = 5;
    addBtn.clipsToBounds = YES;
    addBtn.titleLabel.font = [UIFont systemFontOfSize:12];
    [addBtn addTarget:self action:@selector(addBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    [footer addSubview:addBtn];
    
    self.keywordTF = [[UITextField alloc] initWithFrame:CGRectMake(20, 0, addBtn.left - 28, 44)];
    self.keywordTF.placeholder = @"请输入屏蔽关键词";
    self.keywordTF.textColor = RGB(34, 34, 34);
    self.keywordTF.font = [UIFont systemFontOfSize:14];
    self.keywordTF.clearButtonMode = UITextFieldViewModeWhileEditing;
    [footer addSubview:self.keywordTF];
    
    UIView *line = [[UIView alloc] initWithFrame:CGRectMake(20, 44, WIDTH - 40, 1)];
    line.backgroundColor = RGB(247, 247, 247);
    [footer addSubview:line];
    
    UILabel *messagelLabel = [[UILabel alloc] initWithFrame:CGRectMake(20,line.bottom + 8, WIDTH - 40, 15)];
    messagelLabel.text = @"提示：包含屏蔽关键词的企业无法查看您的简历，请谨慎操作";
    messagelLabel.font = [UIFont boldSystemFontOfSize:11];
    messagelLabel.adjustsFontSizeToFitWidth = YES;
    messagelLabel.textColor = [UIColor orangeColor];
    [footer addSubview:messagelLabel];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    LYPrivacySettingCell *cell = [tableView dequeueReusableCellWithIdentifier:kCellIdentifier_LYPrivacySettingCell forIndexPath:indexPath];
    cell.title = self.dataArray[indexPath.row];
    __weak typeof(self) weakSelf = self;
    cell.block = ^(){
        [weakSelf cancleBtnClick:indexPath.row];
    };
    return cell;
}

#pragma mark -- UITableViewDelegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 45.0;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 0.001;
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 8.0f;
}

#pragma mark -- Action
- (void)cancleBtnClick:(NSInteger)index{
    [self.keywordTF resignFirstResponder];
    [self.dataArray removeObjectAtIndex:index];
    [self removeClick:nil];
}

- (void)addBtnClick:(UIButton *)button{
    [self.keywordTF resignFirstResponder];
    if (self.dataArray.count == self.shieldMax) {
        SVPHUDError(@"已达到公司屏蔽限制");
        return;
    }
    
    NSString *keyword = self.keywordTF.text;
    keyword = [keyword stringByReplacingOccurrencesOfString:@" " withString:@""];
    if (keyword.length == 0) {
        return;
    }
    [self.dataArray addObject:keyword];
    self.keywordTF.text = @"";
    
    NSMutableDictionary *dic = [NSMutableDictionary dictionaryWithCapacity:0];
    dic = [UIToolsControl getPublicParameter];
    dic[@"shield_company"] = self.dataArray;
    __weak typeof(self) weakSelf = self;
    [BaseNetManager requestWithMethod:POST path:Setting_saveShieldCompany_URL params:dic successBlock:^(NSDictionary *responseObject) {
        if ([responseObject[@"code"] integerValue] == 2000) {
            //绑定成功
            __strong typeof(weakSelf) strongSelf = weakSelf;
            strongSelf.block(strongSelf.dataArray);
            [strongSelf.tableview reloadData];
        }else{
            SVPHUDError(responseObject[@"msg"]);
        }
    } failurBlock:^(NSError *error) {
        SVPHUDError(NetErrorAlertText);
    }];
}
- (void)removeClick:(UIButton *)button {
    [self.keywordTF resignFirstResponder];
    
    NSMutableDictionary *dic = [NSMutableDictionary dictionaryWithCapacity:0];
    dic = [UIToolsControl getPublicParameter];
    dic[@"shield_company"] = self.dataArray;
    __weak typeof(self) weakSelf = self;
    [BaseNetManager requestWithMethod:POST path:Setting_saveShieldCompany_URL params:dic successBlock:^(NSDictionary *responseObject) {
        if ([responseObject[@"code"] integerValue] == 2000) {
            //绑定成功
            __strong typeof(weakSelf) strongSelf = weakSelf;
            strongSelf.block(strongSelf.dataArray);
            [strongSelf.tableview reloadData];
        }else{
            SVPHUDError(responseObject[@"msg"]);
        }
    } failurBlock:^(NSError *error) {
        SVPHUDError(NetErrorAlertText);
    }];
}

@end
